<template>
    <div class="block">
        <el-container>
            <el-header >
                <router-link to="/blogs">
                    <img src="../assets/o.jpeg" style="width:100%; height: 60px; margin-top: 0px;">
                </router-link>
            </el-header>
            <el-main>
                <el-form label-position="left" :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                         class="demo-ruleForm" >
                    <el-form-item label="用户名" prop="username">
                        <el-input type="text" maxlength="12" v-model="ruleForm.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item class="sButton">
                        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
            return {
                ruleForm: {
                    password: '111111',
                    username: 'markerhub'
                },
                rules: {
                    password: [
                        {required: true, message: '请输入密码', trigger: 'change'}
                    ],
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                const _this = this;
                _this.$router.push("/locks");
                this.$refs[formName].validate((valid) => {
                    if (valid){
                        //提交
                        this.$axios.post('/login',this.ruleForm).then((res) => {
                            const token = res.headers['authorization'];
                            console.log("======当前用户登录token:"+token);
                            _this.$store.commit('SET_TOKEN',token);
                            _this.$store.commit('SET_USERINFO',res.data.data);
                            _this.$router.push("/locks");
                        });
                    }else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].reset();
            }
        }
    }
</script>

<style scoped>
    .el-header, .el-footer {
        background-color: #B3C0D1;
        color: #333;
        text-align: center;
        line-height: 60px;
    }

    .el-aside {
        background-color: #D3DCE6;
        color: #333;
        text-align: center;
        line-height: 200px;
    }

    .el-main {
        /*background-color: #E9EEF3;*/
        color: #333;
        text-align: center;
        line-height: 160px;
    }

    body > .el-container {
        margin-bottom: 40px;
    }

    .el-container:nth-child(5) .el-aside,
    .el-container:nth-child(6) .el-aside {
        line-height: 260px;
    }

    .el-container:nth-child(7) .el-aside {
        line-height: 320px;
    }

    .demo-ruleForm {
        max-width: 380px;
    }
    .sButton{
        margin-left: -100px !important;
    }

</style>